package com.kejiguan.ui;

import android.app.Activity;
import android.content.Intent;
import android.media.Image;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.android.volley.request.Request;
import com.kejiguan.ui.presenter.LoginActivityPresenter;
import com.kejiguan.ui.response.AlarmResponse;
import com.kejiguan.ui.response.LoginResponse;
import com.kejiguan.ui.util.CacheDataUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * Created by apple on 16/3/2.
 */
public class LoginActivity extends Activity implements View.OnClickListener {

    private EditText userNameInput, passwordInput;
    private ImageView loginBtn;

    private String userName, password;

    private LoginActivityPresenter mPresenter;
    private Request loginRequest;
    private AlarmResponse alarmResponse;

    @Override
    public void onCreate(Bundle bundle) {
        Log.e(LoginActivity.class.getSimpleName(),"onCreate");
        super.onCreate(bundle);
        setContentView(R.layout.activity_login_layout);
        userNameInput = (EditText) findViewById(R.id.username_input);
        passwordInput = (EditText) findViewById(R.id.password_input);
        loginBtn = (ImageView) findViewById(R.id.login_btn);
        loginBtn.setOnClickListener(this);
        mPresenter = new LoginActivityPresenter();
        mPresenter.onCreate(LoginActivity.this);
        EventBus.getDefault().register(this);
        getIntentData();

    }

    @Override
    public void onNewIntent(Intent intent)
    {
        Log.e(LoginActivity.class.getSimpleName(),"onNewIntent");
        super.onNewIntent(intent);
        alarmResponse= (AlarmResponse) intent.getSerializableExtra("alarm_info");

    }

    private void getIntentData()
    {
        alarmResponse= (AlarmResponse) getIntent().getSerializableExtra("alarm_info");
    }

    @Override
    public void onResume()
    {
        super.onResume();
        if (null!= CacheDataUtil.getInstance().getmUser())
        {
            Intent intent=new Intent(LoginActivity.this,MainActivity.class);
            if (null!=alarmResponse)
            {
                intent.putExtra("alarm_info",alarmResponse);
            }
            startActivity(intent);
            finish();

        }
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        mPresenter.onDestroy();
        EventBus.getDefault().unregister(this);

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.login_btn:
                userName = userNameInput.getEditableText().toString().trim();
                password = passwordInput.getEditableText().toString().trim();
                if (TextUtils.isEmpty(userName) || TextUtils.isEmpty(password)) {
                    Toast.makeText(LoginActivity.this, "用户名和密码不能为空", Toast.LENGTH_SHORT).show();
                } else {
                    loginRequest=mPresenter.login(userName,password);
                }
                break;
        }

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(LoginResponse response) {
        if ("0000".equals(response.getRetCode())) {
            Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
            CacheDataUtil.getInstance().setmUser(response.getSysUser());
            CacheDataUtil.getInstance().setDisplayAreas(response.getDisplayAreas());

            Intent intent=new Intent(LoginActivity.this,MainActivity.class);
            startActivity(intent);
            finish();
        } else {
            String errorInfo=TextUtils.isEmpty(response.getRetInfo())?"登录失败":response.getRetInfo();
            Toast.makeText(LoginActivity.this, errorInfo, Toast.LENGTH_SHORT).show();
        }
    }
}
